package com.lucenly.card.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lucenly.card.R;
import com.lucenly.card.adapter.Rank_tab_Adapter;
import com.lucenly.card.base.BaseActivity;
import com.lucenly.card.constants.UrlDefend;
import com.lucenly.card.event.StringMsgEvent;
import com.lucenly.card.fragment.MoneyAFragment;
import com.lucenly.card.fragment.MoneyBFragment;
import com.vise.xsnow.event.IEvent;
import com.vise.xsnow.event.Subscribe;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.utls.SharedPreferencesUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyMoneyActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.tab_FindFragment_title)
    TabLayout tab_FindFragment_title;
    @BindView(R.id.vp_FindFragment_pager)
    ViewPager vp_FindFragment_pager;
    private List<Fragment> list_fragment;                                //定义要装fragment的列表
    private List<String> list_title;
    Rank_tab_Adapter fAdapter;
    MoneyAFragment aFragment;
    MoneyBFragment bFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_money);
    }

    @Override
    protected boolean isRegisterEvent() {
        return true;
    }
    @Subscribe
    public void refresh(IEvent iEvent){
        if(iEvent!=null && iEvent instanceof StringMsgEvent){
            if(((StringMsgEvent) iEvent).getMsg().equals("TIXIAN")){
                getData();
            }
        }
    }
    @Override
    protected void initView() {
        tv_title.setText("我的钱包");
        //将fragment装进列表中
        list_fragment = new ArrayList<>();
         aFragment = new MoneyAFragment();
         bFragment = new MoneyBFragment();
        list_fragment.add(aFragment);
        list_fragment.add(bFragment);
        list_title = new ArrayList<>();
        list_title.add("余额");
        list_title.add("投入");
        //为TabLayout添加tab名称
        tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(0)));
        tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(1)));

        fAdapter = new Rank_tab_Adapter(getSupportFragmentManager(),list_fragment,list_title);

        //viewpager加载adapter
        vp_FindFragment_pager.setAdapter(fAdapter);
        //tab_FindFragment_title.setViewPager(vp_FindFragment_pager);
        //TabLayout加载viewpager
        tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
    }

    @Override
    protected void bindEvent() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        ViseHttp.GET(UrlDefend.PURSE)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            double balanceFee = jsonObject.getDouble("balanceFee");
                            double depositTotalFee = jsonObject.getDouble("depositTotalFee");
                            String withdrawFee = jsonObject.getString("withdrawFee");
                            aFragment.setData(balanceFee,withdrawFee);
                            bFragment.setData(depositTotalFee+"");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }

    @Override
    protected void processClick(View view) {

    }
}
